package org.wattdepot.cli.commands;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Contains methods that list total amount of carbon or energy generated for a given day.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class ListTotalGenerated extends UserCommandInterface implements UserCommands {

  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output;
    try {
      output = listTotalGenerated(inputString[1], inputString[3], inputString[5], inputString[7]);
    }
    catch (WattDepotClientException e) {
      output = "Total Generated error";
     

    }
    return output;
  }

  /**
   * Returns the total amount of carbon (or energy) generated (or consumed) by this source for the
   * given day. The sampling interval indicates how to generate the curve of carbon (or energy) data
   * points.
   * 
   * @param carbonEnergy The total carbon or energy
   * @param sourceName Name of one of the public sources is the system.
   * @param day A date inputed with format YYYY-MM-DD.
   * @param minutes The Sample-interval time in minutes
   * @throws MiscClientException if something goes wrong
   * @throws BadXmlException if something goes wrong
   * @throws ResourceNotFoundException if something goes wrong
   * @throws NotAuthorizedException if something goes wrong
   * @throws NumberFormatException if something goes wrong
   * @return String with the total power returned
   */
  public String listTotalGenerated(String carbonEnergy, String sourceName, String day,
      String minutes) throws NotAuthorizedException, ResourceNotFoundException, BadXmlException,
      MiscClientException {
    // holds the client that will be used
    WattDepotClient client = getClient();
    ValidateDay checkDay = new ValidateDay();
    // start day
    XMLGregorianCalendar startDay = checkDay.validateDay(day);
    // end day
    XMLGregorianCalendar endDay = checkDay.validateDay(day);
    // holds energy string
    double energy;
    // holds carbon string
    double carbon;
    // string that will be output
    StringBuffer buffer = new StringBuffer(256);

    // return to command prompt if source doesn't exist
    if (!sourceExists(sourceName)) {
      // return to command prompt
      return "Source does not exist";
    }

    // return to command prompt if xml is invalid
    if (startDay == null) {
      // reutrn to command prompt
      return "Input a start day.";
    }

    try {

      // makes the endDay 23 hours and 59 minutes after the startDay
      endDay = Tstamp.incrementDays(startDay, 1);
      endDay = Tstamp.incrementMinutes(endDay, -1);
      // the sampleTime
      int sampleTime = Integer.parseInt(minutes);

      // output carbon emitted
      if (carbonEnergy.equalsIgnoreCase("carbon")) {
        carbon = client.getCarbonEmitted(sourceName, startDay, endDay, sampleTime);
        // System.out.println("Carbon Emitted: " + carbon);
        buffer.append("Carbon Emitted: " + carbon + "\n");
      }
      // output energy generated
      else if (carbonEnergy.equalsIgnoreCase("energy")) {
        energy = client.getEnergyGenerated(sourceName, startDay, endDay, sampleTime);
        // System.out.println("Energy Generated: " + energy);
        buffer.append("Energy Generated: " + energy + "\n");
      }
      // if input was not carbon or energy return error message
      else {
        // System.out.println("Must enter \"carbon\" or \"energy\" as input");
        buffer.append("Must enter \"carbon\" or \"energy\" as input.\n");
      }

    }
    catch (MiscClientException exception) {
      System.out.println("Misc Client Exception");
      return "";
    }
    catch (BadXmlException exception) {
      System.out.println("Bad XML Exception");
      return "";
    }
    catch (ResourceNotFoundException exception) {
      System.out.println("Resource Not Found Exception");
      return "";
    }
    catch (NotAuthorizedException exception) {
      System.out.println("Not Authorized Exception");
      return "";
    }
    catch (NumberFormatException exception) {
      System.out.println("Must enter an integer as Sampling Interval parameter.");
      return "";
    }
    return buffer.toString();

  }
}
